import {ConfigEnv, defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import Components from "unplugin-vue-components/vite";
import {resolve} from 'path';
import vueJsx from '@vitejs/plugin-vue-jsx'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
export default defineConfig(({command, mode}: ConfigEnv) => {
    const root = process.cwd();
    const env = loadEnv(mode, root);
    const VITE_BASE_URL = env.VITE_BASE_URL
    return {
        plugins: [vue(), vueJsx(), VueSetupExtend(), Icons({
            compiler: 'vue3',
            autoInstall: true,
        }), Components({
            resolvers: [
                IconsResolver({ prefix: 'icon' }),
                //自动全局导入components文件夹下面的组件
                {
                    // @ts-ignore
                    dirs: ['src/components'],
                    extensions: ['vue']
                }],
        })],
        server: {
            proxy: {
                '/api': {
                    target: VITE_BASE_URL,
                    changeOrigin: true,
                    rewrite: (p) => p.replace(/^\/api/, '')
                }

            }
        },
        resolve: {
            alias: [
                {
                    find: /\/@\//,
                    replacement: pathResolve('src') + '/',
                },
                {
                    find: /\/#\//,
                    replacement: pathResolve('types') + '/',
                }
            ],
        },
        optimizeDeps: {
            include: [
                `monaco-editor/esm/vs/language/json/json.worker`,
                `monaco-editor/esm/vs/language/css/css.worker`,
                `monaco-editor/esm/vs/language/html/html.worker`,
                `monaco-editor/esm/vs/language/typescript/ts.worker`,
                `monaco-editor/esm/vs/editor/editor.worker`
            ],
        },
    }
})

function pathResolve(dir: string) {
    return resolve(process.cwd(), '.', dir);
}
